package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.ArticleBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.zzhou.common.library.util.ToastShow;

import java.util.List;

/**
 * 首页精彩资讯adapter
 * Created by Mr'Tang on 2018/9/10.
 */

public class NewsHomeListAdapter extends android.widget.BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<ArticleBean> mData;

    public NewsHomeListAdapter(Context context, List<ArticleBean> apps) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mData = apps;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ArticleBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_home_news, null);
            viewHolder = new ViewHolder();
            viewHolder.news_img = (ImageView) convertView
                    .findViewById(R.id.news_img);
            viewHolder.news_text = (TextView) convertView
                    .findViewById(R.id.news_text);
            viewHolder.news_date = (TextView) convertView
                    .findViewById(R.id.news_date);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ArticleBean article = getItem(position);
        ImageLoader.loadAds(mContext, Constants.IP + "/" + article.getTopic_img(),
                viewHolder.news_img);
        viewHolder.news_date.setText(article.getTime().substring(0, 10));
        viewHolder.news_text.setText(article.getTopic_title());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                ToastShow.show(mContext,"精彩资讯点击待添加");
//                ArticleActivity.start(mContext, article);
            }

        });
        return convertView;
    }

    class ViewHolder {
        private ImageView news_img;
        private TextView news_text, news_date;
    }
}
